package com.kcht.itsed.mws.subscribescheduler.comm;

import com.kcht.itsed.mws.subscribescheduler.biz.IConcreteReportSender;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPOutputStream;

@Slf4j
public class GZipReportSenderDecorator implements IConcreteReportSender {

	private IConcreteReportSender sender;
	
	public GZipReportSenderDecorator(IConcreteReportSender sender) {
		this.sender = sender;
	}
	
	@Override
	public void send(InputStream reportIS) throws ReportSendFailException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();  
        try (GZIPOutputStream gzip = new GZIPOutputStream(out)){  
            int ch;
            byte[] buffer = new byte[1024];
            while ((ch = reportIS.read(buffer,0,1000)) != -1) {   
                gzip.write(buffer,0,ch);   
                gzip.flush();
            } 
        } catch (IOException e) {
			log.error("",e);
		}
		sender.send(new ByteArrayInputStream(out.toByteArray()));
	}

	@Override
	public ReportSendingState getSendingState() {
		return sender.getSendingState();
	}

}
